package com.jymj.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.jymj.dao.ApplyDao;
import com.jymj.dao.HujiZhaiDao;
import com.jymj.entity.HujiZhaiEntity;
import com.jymj.entity.KeyValueEntity;
import com.jymj.entity.SysUserEntity;
import com.jymj.entity.YlEntity;
import com.jymj.entity.dto.ApplyDto;
import com.jymj.entity.huji.ZhaiEntity;
import com.jymj.entity.model.PointAndTypeEntity;
import com.jymj.entity.zjd.*;
import com.jymj.service.ApplyFileService;
import com.jymj.service.ApplyService;
import com.jymj.service.YlService;
import com.jymj.service.ZhaiService;
import com.jymj.utils.RRException;
import com.jymj.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import static com.jymj.utils.ShiroUtils.getUserEntity;
import static com.jymj.utils.ShiroUtils.getUserId;

/**
 * Created by ljj on 2018/9/12.
 */
@Service
public class ApplyServiceImpl implements ApplyService {
    @Autowired
    private ApplyDao applyDao;
    @Autowired
    private YlService ylService;
    @Autowired
    private ApplyFileService applyFileService;
    @Autowired
    private ZhaiService zhaiService;

    @Override
    public void deleteBatch(Integer[] ylIds) {
        applyDao.deleteBatch(ylIds);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void save(ApplyEntity apply) {
            //判断是否申请过翻建
            ApplyEntity applyEntity = applyDao.getIdCad(apply.getZhaiEntity().getIdCard());
            if (applyEntity != null){
                throw new RRException("该人员已申请翻建");
            }
            //判断图片是否完全
            pictureJudge(apply.getSptype(),apply.getApplyFileList());

            apply.setYlId(apply.getYlEntity().getObjectid());
            apply.setSqDate(new Date());
            apply.setUserId(Integer.parseInt(getUserId().toString()));
            apply.setCode(apply.getYlEntity().getCode());
            apply.setXzqmc(apply.getYlEntity().getXzqmc());
            apply.setZhen(apply.getYlEntity().getTownname());
            apply.setName(apply.getZhaiEntity().getHousecount());
            apply.setApUserId(apply.getZhaiEntity().getZhaiid());
            apply.setIphone(apply.getZhaiEntity().getPhone());
            applyDao.saveEntity(apply);
            //修改院落状态
            YlEntity ylEntity = apply.getYlEntity();
            ylEntity.setApptype(1);
            ylService.updateYl(ylEntity);
            saveOrUpdate(apply);
    }

    private void pictureJudge(Integer sptype, List<ApplyFileEntity> applyFileList) {
        List<Integer> int1 = new ArrayList<>();
        List<Integer> collect = applyFileList.stream()
                .map(ApplyFileEntity::getCltype).distinct().sorted().collect(Collectors.toList());
        if (sptype == 2){
            Integer[] arr = {1,2,3,4};
            int1 = Arrays.asList(arr);
            if(!collect.equals(int1)){
                throw new RRException("资料不全");
            }
        }
        if (sptype == 3){
            Integer[] arr = {1,2,3,4,5,6,7,13};
            int1 = Arrays.asList(arr);
            if(!collect.equals(int1)){
                throw new RRException("资料不全");
            }
        }
        if (sptype == 4){
            Integer[] arr = {1,2,3,4,5,6,7,13,8};
            int1 = Arrays.asList(arr);
            if(!collect.equals(int1)){
                throw new RRException("资料不全");
            }
        }
        if (sptype == 5){
            Integer[] arr = {1,2,3,4,5,6,7,13,9};
            int1 = Arrays.asList(arr);
            if(!collect.equals(int1)){
                throw new RRException("资料不全");
            }
        }
        if (sptype == 6){
            Integer[] arr = {1,2,3,4,5,6,7,8,10,11,13};
            int1 = Arrays.asList(arr);
            if(!collect.equals(int1)){
                throw new RRException("资料不全");
            }
        }
        if (sptype == 7){
            Integer[] arr = {1,2,3,4,5,6,7,8,10,11,12,13};
            int1 = Arrays.asList(arr);
            if(!collect.equals(int1)){
                throw new RRException("资料不全");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void update(ApplyEntity apply) {
            ApplyEntity applyEntity = applyDao.queryObject(apply.getId());
            //判断图片是否完全
            if (applyEntity.getSptype() != apply.getSptype()){
                pictureJudge(apply.getSptype(),apply.getApplyFileList());
            }

            apply.setCode(apply.getYlEntity().getCode());
            apply.setXzqmc(apply.getYlEntity().getXzqmc());
            apply.setZhen(apply.getYlEntity().getTownname());
            apply.setName(apply.getZhaiEntity().getHousecount());
            apply.setIphone(apply.getZhaiEntity().getPhone());
            applyDao.update(apply);
            //修改院落状态
            YlEntity ylEntity = apply.getYlEntity();
            ylEntity.setApptype(apply.getSptype());
            ylService.updateYl(ylEntity);
            saveOrUpdate(apply);
    }

    public void saveOrUpdate(ApplyEntity apply){
        //添加翻建房屋信息
        if (apply.getApplyFwEntities() != null && apply.getApplyFwEntities().size() > 0) {
            for (ApplyFwEntity applyFwEntity : apply.getApplyFwEntities()) {
                List<ApplyFwEntity> applyFwEntities = new ArrayList<>();
                if (applyFwEntity.getId() != null && applyFwEntity.getId() != 0) {
                    applyDao.updateApplyFw(applyFwEntity);
                } else {
                    applyFwEntities.add(applyFwEntity);
                    applyDao.saveApplyFw(applyFwEntities, apply.getId(), apply.getYlId(),-1);
                }
            }
        }
        //修改翻建图片
        if (apply.getApplyFileList() != null && apply.getApplyFileList().size() > 0) {
            for (ApplyFileEntity applyFileEntity : apply.getApplyFileList()) {
                applyFileEntity.setAppId(apply.getId());
                applyFileEntity.setYlId(apply.getYlId());
                applyFileService.update(applyFileEntity);
            }
        }
        //修改户籍信息
        if (apply.getZhaiEntities() != null && apply.getZhaiEntities().size() > 0) {
            Integer flag = 0;
            for (ZhaiEntity zhaiEntity : apply.getZhaiEntities()) {
                if (zhaiEntity.getIdCard() != null && apply.getZhaiEntity().getIdCard() != null) {
                    if (zhaiEntity.getIdCard().equals(apply.getZhaiEntity().getIdCard())) {
                        zhaiEntity.setFjhz(1);
                        flag += 1;
                    }
                }
            }
            if (flag <= 0) {
                apply.getZhaiEntities().add(apply.getZhaiEntity());
            }
            for (ZhaiEntity zhaiEntity : apply.getZhaiEntities()) {
                //判断添加  修改
                zhaiEntity.setNoteTaker(String.valueOf(getUserId()));
                zhaiEntity.setYlId(apply.getYlId());
                zhaiEntity.setCode(apply.getYlEntity().getCode());
                zhaiEntity.setZhen(apply.getYlEntity().getTownname());
                if (zhaiEntity.getZhaiid() != null && zhaiEntity.getZhaiid() != 0) {
                    zhaiService.update(zhaiEntity);
                } else {
                    zhaiService.save(zhaiEntity);
                }
            }
        } else {
            apply.getZhaiEntity().setNoteTaker(String.valueOf(getUserId()));
            apply.getZhaiEntity().setYlId(apply.getYlId());
            apply.getZhaiEntity().setCode(apply.getYlEntity().getCode());
            apply.getZhaiEntity().setZhen(apply.getYlEntity().getTownname());
            if (apply.getZhaiEntity().getZhaiid() != null) {
                zhaiService.update(apply.getZhaiEntity());
            } else {
                zhaiService.save(apply.getZhaiEntity());
            }
        }
    }

    @Override
    public List<ApplyEntity> queryList(ApplyDto applyDto) {
        return applyDao.queryList(applyDto);
    }

    @Override
    public ApplyEntity queryObject(Integer applyId) {
        ApplyEntity applyEntity = applyDao.queryObject(applyId);
        applyEntity.setZhaiEntity(zhaiService.getzhai(applyEntity.getYlId(),applyEntity.getHousehold()));
//        applyEntity.getZhaiEntities().removeAll(Collections.singleton(null));
        return applyEntity;
    }

    @Override
    public List<ApplyEntity> queryObjectByylId(Integer objectid) {
        return applyDao.queryObjectByylId(objectid);
    }

    @Override
    public ZjdCountEntity queryAllCount(String point, String code) {
        return applyDao.queryAllCount(point,code);
    }

    @Override
    public List<KeyValueEntity> getSummary(ApplyDto applyDto) {
        applyDto.setSysUser(getUserEntity());
        List<KeyValueEntity> keyValueEntities = applyDao.getSummary(applyDto);
        KeyValueEntity keyValueEntity = new KeyValueEntity();
        keyValueEntity.setObject1("总计");
        keyValueEntity.setObject3(keyValueEntities.stream().mapToInt(z ->
                z.getObject3() == null ? 0 : Integer.valueOf(z.getObject3().toString())).sum());
        keyValueEntity.setObject4(keyValueEntities.stream().mapToInt(z ->
                z.getObject4() == null ? 0 : Integer.valueOf(z.getObject4().toString())).sum());
        keyValueEntity.setObject5(keyValueEntities.stream().mapToInt(z ->
                z.getObject5() == null ? 0 : Integer.valueOf(z.getObject5().toString())).sum());
        keyValueEntity.setObject6(keyValueEntities.stream().mapToInt(z ->
                z.getObject6() == null ? 0 : Integer.valueOf(z.getObject6().toString())).sum());
        keyValueEntity.setObject7(keyValueEntities.stream().mapToInt(z ->
                z.getObject7() == null ? 0 : Integer.valueOf(z.getObject7().toString())).sum());
        keyValueEntity.setObject8(keyValueEntities.stream().mapToInt(z ->
                z.getObject8() == null ? 0 : Integer.valueOf(z.getObject8().toString())).sum());
        keyValueEntity.setObject9(keyValueEntities.stream().mapToInt(z ->
                z.getObject9() == null ? 0 : Integer.valueOf(z.getObject9().toString())).sum());
        keyValueEntities.add(0,keyValueEntity);
        return keyValueEntities;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteFjfw(Long[] ids) {
        int i = applyDao.deleteFjfw(ids);
        if (i <= 0){
            throw new RRException("删除失败");
        }
    }

    @Override
    public KeyValueEntity queryStatistical(ApplyDto applyDto) {
        return applyDao.queryStatistical(applyDto);
    }

    @Override
    public List<ApplyCountEntity> queryCount1(PointAndTypeEntity pointAndTypeEntity) {
        return applyDao.queryCount1(pointAndTypeEntity);
    }

    @Override
    public List<ApplyCountEntity> queryCount2(PointAndTypeEntity pointAndTypeEntity) {
        return applyDao.queryCount2(pointAndTypeEntity);
    }

    @Override
    public List<ApplyCountEntity> queryCount3(PointAndTypeEntity pointAndTypeEntity) {
        return applyDao.queryCount3(pointAndTypeEntity);
    }

    @Override
    public List<ApplyCountEntity> queryCount4(PointAndTypeEntity pointAndTypeEntity) {
        return applyDao.queryCount4(pointAndTypeEntity);
    }
}
